let app = getApp();

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    kitap_test: [],
    kitap_test1: [],
    kitap_test2: [],
    kitap_test3: [],
    recommendList: [],
    week_list: [
      { weekday_number: 1, name: 'دۈ', isActive: false },
      { weekday_number: 2, name: 'سە', isActive: false },
      { weekday_number: 3, name: 'چا', isActive: false },
      { weekday_number: 4, name: 'پە', isActive: false },
      { weekday_number: 5, name: 'جۈ', isActive: false },
      { weekday_number: 6, name: 'شە', isActive: false },
      { weekday_number: 7, name: 'يە', isActive: false },
    ],
    bookType: [
      { id: 1, name: 'ھېكايە' },
      { id: 2, name: 'شىئېر' },
      { id: 3, name: 'بالىلار' },
      { id: 4, name: 'رومان' },
      { id: 5, name: 'بارلىق' },
    ],
    isLoading: true, // 是否加载完成
    pageNo: 1,
    pageSize: 6,
    status: 1,
    recommend: '',
    is_free: '',
    video_source: '',
    isLanguageTypeValue: '',
    keyWord: '',
    categoryId: '',
    previewBook: [],
    sunWuKongImageUrl: 'https://mmecoa.qpic.cn/sz_mmecoa_jpg/iauAIibMdU78vgvbvibfuMAXFoAkDMDfNvtyB9ejSuEKu2rgDpBzeAfrkr7zPiaVMzHib9f9cBlMoNSGUXO4c2VuicYQ/640?wx_fmt=jpeg&amp;from=appmsg',
    sanGuoImageUrl: 'https://mmecoa.qpic.cn/sz_mmecoa_jpg/iauAIibMdU78vgvbvibfuMAXFoAkDMDfNvtAg5acu8cOZcFBZ1ODgP4ZylJC2D86JlXe0Svs5uH637tshy8HfN9wg/640?wx_fmt=jpeg&amp;from=appmsg',

    userInfo: {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取首页内容
    getPost() {
      let that = this;
      wx.request({
        url: `${app.globalData.API_URL}/kitap`,
        data: {
          pageNo: this.data.pageNo,
          pageSize: this.data.pageSize,
          status: this.data.status,
          recommend: this.data.recommend,
          is_free: this.data.is_free,
          categoryId: this.data.categoryId,
          video_source: this.data.video_source,
          isLanguageTypeValue: this.data.isLanguageTypeValue,
          keyWord: this.data.keyWord,
          bolum_id: 1
        },
        header: {
          'content-type': 'application/json'
        },
        success (res) {
          if(res.statusCode == 200) {
            that.setData({ kitap_test: res.data.data })
          }
        }
      });

      this.getBooks1();
      this.getBooks2();
      this.getBooks3();
      this.onGetRecommend();
    },

    // kitap_test1
    getBooks1() {
      let that = this;
      wx.request({
        url: `${app.globalData.API_URL}/kitap`,
        data: {
          pageNo: this.data.pageNo,
          pageSize: this.data.pageSize,
          status: this.data.status,
          recommend: this.data.recommend,
          is_free: this.data.is_free,
          categoryId: this.data.categoryId,
          video_source: this.data.video_source,
          isLanguageTypeValue: this.data.isLanguageTypeValue,
          keyWord: this.data.keyWord,
          bolum_id: 3
        },
        header: {
          'content-type': 'application/json'
        },
        success (res) {
          if(res.statusCode == 200) {
            that.setData({ kitap_test1: res.data.data })
            that.LoadingFinish(); // 完成加载
          }
        }
      })
    },

    // kitap_test2
    getBooks2() {
      let that = this;
      wx.request({
        url: `${app.globalData.API_URL}/kitap`,
        data: {
          pageNo: this.data.pageNo,
          pageSize: this.data.pageSize,
          status: this.data.status,
          recommend: this.data.recommend,
          is_free: this.data.is_free,
          categoryId: this.data.categoryId,
          video_source: this.data.video_source,
          isLanguageTypeValue: this.data.isLanguageTypeValue,
          keyWord: this.data.keyWord,
          bolum_id: 2
        },
        header: {
          'content-type': 'application/json'
        },
        success (res) {
          if(res.statusCode == 200) {
            that.setData({ kitap_test2: res.data.data })
            that.LoadingFinish(); // 完成加载
          }
        }
      });
    },

    // kitap_test3
    getBooks3() {
      let that = this;
      wx.request({
        url: `${app.globalData.API_URL}/kitap`,
        data: {
          pageNo: this.data.pageNo,
          pageSize: this.data.pageSize,
          status: this.data.status,
          recommend: this.data.recommend,
          is_free: this.data.is_free,
          categoryId: this.data.categoryId,
          video_source: this.data.video_source,
          isLanguageTypeValue: this.data.isLanguageTypeValue,
          keyWord: this.data.keyWord,
          bolum_id: 0
        },
        header: {
          'content-type': 'application/json'
        },
        success (res) {
          if(res.statusCode == 200) {
            that.setData({ kitap_test3: res.data.data })
            that.LoadingFinish(); // 完成加载
          }
        }
      });
    },

    // 推荐
    onGetRecommend() {
      let that = this;
      wx.request({
        url: `${app.globalData.API_URL}/recommend`,
        header: {
          'content-type': 'application/json'
        },
        success (res) {
          if(res.statusCode == 200) {
            that.setData({ recommendList: res.data })
            that.LoadingFinish(); // 完成加载
          }
        }
      });
    },

    // 点击类目
    onCategory() {
      wx.navigateTo({
        url: '/pages/category/category',
      })
    },

    // 点击更多
    onClickMore(item) {
      let bolum_id = item.currentTarget.dataset.item;
      wx.navigateTo({
        url: `/pages/books/books?bolum_id=`+bolum_id,
      })
    },

    // 点击签到
    onSignIn(item) {
      wx.navigateTo({
        url: '/pages/signIn/signIn',
      })
    },

    // 获取预览
    getPreview () {
      let userInfo = wx.getStorageSync('userInfo');
      let user_id;
      if(!userInfo) {
        user_id = 1;
      } else {
        user_id = userInfo.user.id;
      }
      
      let data = { user_id: user_id, pageNo: this.data.pageNo, pageSize: this.data.pageSize }
      let that = this;
      wx.request({
        url: `${app.globalData.API_URL}/getPreview`,
        method: 'POST',
        data: data,
        header: {
          'content-type': 'application/json',
        },
        success(res) {
          if (res.statusCode) {
            that.setData({ previewBook: res.data.data });
            that.LoadingFinish(); // 完成加载
          }
        },
      });
    },

    // 点击预览书
    onPreview(item) {
      let postId = item.currentTarget.dataset.item.kitap.id;
      wx.navigateTo({
        url: `/pages/audioList/audioList?postId=`+postId,
      })
    },

    // 点击推荐书
    onRecommend(item) {
      let postId = item.currentTarget.dataset.item.id;
      wx.navigateTo({
        url: `/pages/audioList/audioList?postId=`+postId,
      })
    },

    // 更改推荐书
    onChangeRecommend() {
      console.log("更改 ...")
    },

    // 检查会员 getUserAuth
    checkVIP() {
      let userInfo = wx.getStorageSync('userInfo')
      let user_id = userInfo.user.id;
      let data = { user_id: user_id }
      wx.request({
        url: `${app.globalData.API_URL}/getUserAuth`,
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        data: data,
        success: res => {
          console.log("res::: ", res)
        }
      })
    },

    // 签到
    createSign() {
      let userInfo = wx.getStorageSync('userInfo');
      let user_id;
      if(!userInfo) return;
      user_id = userInfo.user.id;
      
      let data = { user_id: user_id }
      let that = this;
      wx.request({
        url: `${app.globalData.API_URL}/sign`,
        method: 'POST',
        data: data,
        header: {
          'content-type': 'application/json',
        },
        success(res) {
          if (res.statusCode) {
            let week_list = that.data.week_list;
            for(let item of week_list) {
              for (let e of res.data.weekSigns) {
                item.weekday_number == e.weekday_number ? item.isActive = true : ''
              }
            }
            that.setData({ week_list });
          }
        },
      });
    },

    // 加载完成
    LoadingFinish() {
      setTimeout(() => {
        this.setData({ isLoading: false })
      }, 300)
    },

  },
  
  lifetimes: {
    attached: function() {
      this.getPost();
      this.getPreview();
      this.createSign();
    },
    created() {},
  }
})